﻿using System;
using HK.Core.Common.Data;
using HK.Core.Common.Protocol.Data;
using HK.Core.GUIEx.Protocol.Attribute;
using HK.Core.Serializable.Attribute;
using HK.Core.Serializable.Data;
using HK.Core.Serializable.Protocol.Data;
using HK.Editor.Settings.Protocol.Data;
using UnityEngine;

namespace HK.Editor.Settings.Data
{
    /// <summary>
    /// App打包配置
    /// </summary>
    [Serializable]
    public sealed class BuildAppConf : JsonData<BuildAppConf>, IBuildAppConf
    {

#region Create

        /// <summary>
        /// 创建App打包配置
        /// </summary>
        /// <returns>App打包配置</returns>
        public static BuildAppConf Create() => new BuildAppConf();

#endregion
        
        /// <summary>
        /// 保护构造函数
        /// </summary>
        private BuildAppConf() {}
        
        [SerializeField, SVersion("版本", (int)GUIOperateOptions.ExpandableEdit)]
        private SAssetVersion version = SAssetVersion.Create();
        /// <summary>
        /// App版本号
        /// </summary>
        public ISAssetVersion Version => version;

        /// <summary>
        /// 重置版本
        /// </summary>
        /// <param name="iVersion">版本</param>
        /// <returns>true:有变化; false:无变化;</returns>
        public bool ResetVersion(string iVersion)
        {
            var newVersion = SAssetVersion.Decode(iVersion);
            if (null == newVersion || !newVersion.Valid) return false;

            return version.ApplyData(newVersion);
        }

        /// <summary>
        /// 应用数据
        /// </summary>
        /// <param name="iData">数据</param>
        /// <param name="iImport">导入标志位(true:从相应的*.json文件导入; false:反之;)</param>
        /// <returns>true:有变更; false:无变更;</returns>
        public override bool ApplyData(IJsonData iData, bool iImport = false)
        {
            if (null == iData || !iData.Valid) return false;

            var dirty = base.ApplyData(iData, iImport);
            if (dirty && !iImport)
            {
                Dirty = true;
            }

            if (iData is IBuildAppConf conf)
            {
                dirty = version.ApplyData(conf.Version);
                if (dirty && !iImport)
                {
                    Dirty = true;
                }
            }

            return Dirty;
        }

        /// <summary>
        /// 清空
        /// </summary>
        public override void Clear()
        {
            base.Clear();
            
            version.Clear();
        }
    }
}
